#include "HIM.h"
#include <iostream>


HIM::HIM(void)
{
}


HIM::~HIM(void)
{
}

void HIM::Load( char* a_FilePath )
{
	//File handler for further reading
	FILE* FileHandler = fopen( a_FilePath, "rb" );
	//File reading
	fread( &m_Width, sizeof( m_Width ), 1, FileHandler );
	fread( &m_Height, sizeof( m_Height ), 1, FileHandler );
	fread( &m_GridCount, sizeof( float ), 1, FileHandler );
	
	for ( int i = 0; i < m_Width; i++ )
	{
		for ( int j = 0; j < m_Height; j++ )
		{
			fread( &m_HeightMap[i][j], sizeof(float), 1, FileHandler );
			printf("%i , %i : %f\n", i, j, m_HeightMap[i][j]);
			m_HeightMap[i][j] /= 100;
		}
	}

	//Under construction
	/*char string[512];

	unsigned int length;
	unsigned char curBLen;
	fread(&curBLen, 1, 1, FileHandler );
	length = curBLen & 0x7F;
	if(curBLen & 0x80){
	fread(&curBLen, 1, 1, FileHandler );
	length |= (curBLen & 0x7F) << 7;
	}
	fread( string, length, 1, FileHandler );
	string[length] = 0;

	int check =  strcmp( string, "quad" );

	fread( &m_QuadEntryCount, sizeof ( int ), 1,FileHandler );*/

	/*m_QuadEntries = new QuadEntry[ m_QuadEntryCount ];
	for ( int i = 0; i < m_QuadEntryCount; i++ )
	{
		fread( &m_QuadEntries[i].m_MinZ, sizeof( float ), 1, FileHandler );
		fread( &m_QuadEntries[i].m_MaxZ, sizeof( float ), 1, FileHandler );
	}

	fread( &m_QuadTreeCount, sizeof( m_QuadTreeCount ), 1, FileHandler );*/

	fclose( FileHandler );

	//fread( &m_QuadCount, sizeof( m_QuadCount ), 1, FileHandler );

}
extern "C" __declspec(dllexport) 
void __stdcall Checkie()
{

}

extern "C" __declspec(dllexport) 
extern void __stdcall HIM::Save( char* a_FilePath )
{
	FILE* FileHandler = fopen( a_FilePath, "wb" );
	fwrite( &m_Width, sizeof( m_Width ), 1, FileHandler );
	fwrite( &m_Height, sizeof ( m_Height ), 1, FileHandler );
	fwrite( &m_GridCount, sizeof( m_GridCount ), 1, FileHandler );
	
	for ( int i = 0; i < m_Width; i++ )
	{
		for ( int j = 0; j < m_Height; j++ )
		{
			m_HeightMap[i][j] *= 100;
			fwrite( &m_HeightMap[i][j], sizeof( float ), 1, FileHandler );
		}
	}

	

	fclose( FileHandler );
}

float HIM::GetHeight( int a_X, int a_Y )
{
	return m_HeightMap[a_X][a_Y];
}
